package com.tianpengtech.admin.websitead;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.admin.AbstractAdmin;
import com.tianpengtech.common.model.websitead.WebSiteAd;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.string.StringMustBeNumberic;
import com.tianpengtech.common.validator.rules.string.StringMustBeRequired;

public class WebSiteAdAdminServiceImpl extends AbstractAdmin{

	@Override
	public void beforeAdd(Model<?> model) throws ValidateException {

		Validator validator = new Validator();
		String category = model.getStr(WebSiteAd.CATEGORY);
		String url = model.getStr(WebSiteAd.URL);
		String name = model.getStr(WebSiteAd.NAME);
		int sort = model.getInt(WebSiteAd.SORT);
		validator.addRule(new StringMustBeRequired(name, "请填写图片名称"));
		validator.addRule(new StringMustBeRequired(category, "请选择图片所属分类"));
		validator.addRule(new StringMustBeRequired(url, "请选择图片"));
		validator.addRule(new StringMustBeNumberic(sort+"", "排序必须是数字"));
		validator.excute();
	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model<?> model) throws ValidateException {
		Validator validator = new Validator();
		String category = model.getStr(WebSiteAd.CATEGORY);
		String url = model.getStr(WebSiteAd.URL);
		String name = model.getStr(WebSiteAd.NAME);
		int sort = model.getInt(WebSiteAd.SORT);
		validator.addRule(new StringMustBeRequired(name, "请填写图片名称"));
		validator.addRule(new StringMustBeRequired(category, "请选择图片所属分类"));
		validator.addRule(new StringMustBeRequired(url, "请选择图片"));
		validator.addRule(new StringMustBeNumberic(sort+"", "排序必须是数字"));
		validator.excute();
	}

	@Override
	public void beforeDelete(Model<?> model) {
		
	}

	@Override
	public void builderSQLExpected(Model< ? > model,
			QueryConstructor queryConstruct, Map< String, Object > params) {
		String category = model.getStr(WebSiteAd.CATEGORY);
		if(StringUtils.isNotBlank(category)){
			queryConstruct.and(new EqualCondition(WebSiteAd.CATEGORY, category));
		}
	}

	@Override
	public String tableName() {
		return WebSiteAd.dao.tableName();
	}

}